// pages/my/my.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        login: false,
        userInfo: '',
        list:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    getUserInfo: function(e) {
        var that = this;
        wx.getSetting({
            success(res) {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    // 登录
                    wx.login({
                        success: res => {
                            // 发送 res.code 到后台换取 openId, sessionKey, unionId
                            console.log('登录' + res.code)
                            wx.request({
                                method: 'Get',
                                url: app.globalData.host + 'api/VerCode/LoginByWeChatCallback',
                                data: {
                                    code: res.code, //告诉后台此微信用户的唯一标识
                                },
                                success: res => { //登录成功了 
                                    console.log(res.data)
                                    wx.setStorageSync('openId', res.data)
                                  var data1 = {
                                    open_id: wx.getStorageSync('openId')
                                  }
                                  app.request('api/VerCode/GetOrders', data1, 'Get',
                                    (result) => {
                                      result.data.forEach(function(item){
                                        item._yuyue_time = item._yuyue_time.replace('T',' ')
                                        item._add_time = item._add_time.replace('T', ' ')
                                      })
                                      that.setData({
                                        list: result.data
                                      })

                                    }
                                  );

                                      wx.getUserInfo({
                                          success: function (res) {
                                              wx.setStorageSync('userInfo', res.userInfo)
                                              that.setData({
                                                  userInfo: wx.getStorageSync('userInfo'),
                                                  login: true,
                                              })
                                              var data = {   //创建用户
                                                  openId: wx.getStorageSync('openId'),
                                                  nickname: res.userInfo.nickName,
                                                  head_url: res.userInfo.avatarUrl,
                                              }
                                              app.request('api/VerCode/AddUser', data, 'Get',
                                                  (result) => {
                                                      console.log(result);
                                                  }
                                              );
                                          }
                                      })
                                }
                            })


                        }
                    })




                } else {

                }
            }
        })



    },
    onLoad: function(options) {
        var that = this;
        if (wx.getStorageSync('userInfo')) {
            that.setData({
                login: true,
                userInfo: wx.getStorageSync('userInfo')
            })
        }



    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      if (wx.getStorageSync('openId')){
        var data = {
          open_id: wx.getStorageSync('openId')
        }
        app.request('api/VerCode/GetOrders', data, 'Get',
          (result) => {
            result.data.forEach(function (item) {
              item._yuyue_time = item._yuyue_time.replace('T', ' ')
              item._add_time = item._add_time.replace('T', ' ')
            })
            this.setData({
              list: result.data
            })
          }
        );
      }
      
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    toOrderList: function() {
        wx.navigateTo({
            url: '/pages/orderList/orderList',
        })
    },
    toAbout: function() {
        wx.navigateTo({
            url: '/pages/about/about',
        })
    }
})